#include <regx52.h>
int time=0;
int time_seed=0;
unsigned int i;
int count=0;
/**
 * 等待用户出拳，点击按钮代表出拳石头剪刀布。系统随机出拳。
 * 决出胜负，胜利方灯亮。点击按钮重新开始游戏。
 */

void timer0_init()
{
    TMOD &= 0xF0;
    TMOD |= 0x01;

    TH0 = 0xDB;

    TL0 = 0xFF;

    TR0 = 1;

    ET0 = 1;

    EA = 1;
    
    PT0 = 1;
}
void timer0_ISR() interrupt 1
{
  TH0 = 0xDB;  
  TL0 = 0xFF;
  time=time+1;
}
unsigned int timer_rand()
{
    time_seed = (time * 123 + 3) % 3;

    return time_seed;
}
void receive(unsigned int x)
{
  if(x==0)
  {
   P2_4=0;
   P2_5=1;
   P2_6=1;
  }
  if(x==1)
  {
   P2_5=0;
   P2_4=1;
   P2_6=1;
  }
  if(x==2)
  {
   P2_6=0;
   P2_5=1;
   P2_4=1;
  }
}
void game()
{
  unsigned int x;
 if(P1_1==0)
 {
  while(P1_1==0){}
   P2_1=0;
   P2_2=1;
   P2_3=1;
   x=timer_rand();
   receive(x);
   if(x==0)
   {
    P2_7=0;
   }
   if(x==1)
   {
    P2_0=0;
   }
 }
 if(P1_2==0)
 {
   while(P1_2==0){}
   P2_2=0;
   P2_1=1;
   P2_3=1;
   x=timer_rand();
   receive(x);
   if(x==2)
   {
    P2_7=0;
   }
   if(x==0)
   {
    P2_0=0;
   }
 }
 if(P1_3==0)
 {
    while(P1_3==0){}
    P2_3=0;
    P2_1=1;
    P2_2=1;
    x=timer_rand();
    receive(x);
    if(x==1)
    {
     P2_7=0;
    }
    if(x==2)
    {
     P2_0=0;
    }
 }
}

void start()
{
  if(P1_0==0)
  {
    while(P1_0==0)
    {
    }
    count=count+1;
  }
  if(count==1)
  {
    game();
  }
  if(count==2)
  {
   count=0;
   P2=0xFF;
  }
}


void main()
{
    timer0_init();
    while(1)
    {
    start();

    }
}
